#include "main.h"
#include "Global.h"
#include "Functions.h"
#include "CLevel.h"
#include "CVObject.h"
#include "CBlock.h"
#include "CPlayer.h"
#include "Collision.h"


CEntityList* g_pEntList = NULL;




CEntity* toEntity(string _line){
	ENTITY_PARAMS Params;
	ZeroMemory(&Params,sizeof(ENTITY_PARAMS));
	int objectid=toInt(_line.substr(0, _line.find_first_of('|')));
	_line=_line.substr(_line.find_first_of('|')+1);//# of parameters
	_line=_line.substr(_line.find_first_of('|')+1);
	float x=(float)toInt(_line.substr(0, _line.find_first_of('|')));
	_line=_line.substr(_line.find_first_of('|')+1);
	float y=(float)toInt(_line.substr(0, _line.find_first_of('|')));
	_line=_line.substr(_line.find_first_of('|')+1);
	//float w=(float)toInt(_line.substr(0, _line.find_first_of('|')));
	//_line=_line.substr(_line.find_first_of('|')+1);
	//float h=(float)toInt(_line.substr(0, _line.find_first_of('|')));
	//_line=_line.substr(_line.find_first_of('|')+1);
	switch(objectid){
	case 100:
		Params.pos=V(x+16, y+16);
		Params.width=26;
		Params.height=16;
		Params.accel=V0;
		Params.stateflags=CEntity::EmptyState;
		Params.depth=0;
		return g_pPlayer = new CPlayer(&Params);
	//case 101:
		//return new CObject();
	//case 102:
		//return new CVObject();
	case 103:
		Params.pos=V(x+16, y+16);
		Params.width=32;
		Params.height=32;
		Params.accel=V0;
		Params.stateflags=CEntity::Solid | CEntity::Immovable | CEntity::Invincible;
		Params.depth=0;
		return new CBlock(&Params, NULL);
	default:
		return NULL;
	}
	return NULL;
}

CLevel::CLevel(){
	 m_pEntList = new CEntityList();
	 g_pEntList = m_pEntList;
	 m_pCList= new LinkedList<CConstraint>();
	 m_bLoaded=false;
};
CLevel::~CLevel(){};
void CLevel::LoadLevel(LPCSTR filename){
	string line;
	ifstream file(filename);
	//file.open ;
	if(file.is_open()){
		getline(file, line);
		LEVEL_WIDTH=toInt(line);
		getline(file, line);
		LEVEL_HEIGHT=toInt(line);
		getline(file, line);
		getline(file, line);
		while(!file.eof()){
			getline(file, line);
			if(line!="")
				m_pEntList->add(toEntity(line));
		}
	}

	g_pCollisionMan->SetSize(LEVEL_WIDTH,LEVEL_HEIGHT);

	g_bSortList=true;
	m_bLoaded=true;
};
void CLevel::DumpLevel(){
	m_pBackground=NULL;
	m_pEntList->clear();
	m_pEntList->Init();
	m_pCList->clear();
	m_pCList->Init();
	m_bLoaded=false;
};
/*-------------------------------------------------------------------
-Level Step
-------------------------------------------------------------------*/
void CLevel::Step(BYTE _keys[256], DIMOUSESTATE _mouse){
	m_pEntityIt = m_pEntList->first();
	while(m_pEntityIt!=NULL){
		m_pEntity = m_pEntityIt->object;
		if(m_pEntity){
			m_pEntity -> Step(m_pEntList, _keys, _mouse); //the objects reoccuring event (Time Step)

			if (m_pEntity->getState() & CEntity::Dead) //deadflag set?
				m_pEntityIt = m_pEntList->remove(m_pEntityIt);
		}
		m_pEntityIt = m_pEntityIt -> next;
	}
	g_pCollisionMan->DoCollisions();
	MOUSE.x=(float)g_cursorPos.x+CAMERA.x;
	MOUSE.y=(float)g_cursorPos.y+CAMERA.y;
	//Satisfy Constraints
	m_pConstraintIt = m_pCList->first();
	while(m_pConstraintIt!=NULL){
		m_pConstraint = m_pConstraintIt->object;
		if(m_pConstraintIt->object!=NULL)
			m_pConstraint ->Satisfy();
		m_pConstraintIt = m_pConstraintIt -> next;
	}
};
/*-------------------------------------------------------------------
-Level Draw
-------------------------------------------------------------------*/
void CLevel::Draw(){
	if(g_bSortList){
		SortEntList(m_pEntList);
		g_bSortList=false;
	}
	//if(Background!=NULL)
		//g_pDrawSprite(Background, 0, Background->width/2, Background->height/2);
	m_pEntityIt = m_pEntList->first();
	while(m_pEntityIt!=NULL){
		m_pEntity = m_pEntityIt->object;
		if(m_pEntity)
			m_pEntity -> Draw(g_pSpriteList);
		m_pEntityIt = m_pEntityIt -> next;
	}
};

void CLevel::CleanUp(){
	delete m_pEntList;
};

